package com.zx.web.dao.impl;

import com.zx.bean.GoodsType;
import com.zx.utils.DataSourceUtils;
import com.zx.web.dao.AdminGoodsTypeDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.dao.impl
 * @Description:
 * @Date 2022/9/16 14:11
 */
public class AdminGoodsTypeDaoImpl implements AdminGoodsTypeDao {
    @Override
    public List<GoodsType> findGoodType() {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_goods_type";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanListHandler<>(GoodsType.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public GoodsType findGoodTypeParent(int parent) {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_goods_type where id = ?";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanHandler<>(GoodsType.class),parent);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void addGoodsType(int goodsParent, String typename, int level) {
        QueryRunner qr = new QueryRunner();
        String sql = "INSERT INTO `tb_goods_type` (`id`, `name`, `level`, `Parent`) VALUES (null, ?, ?, ?);";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,typename,level+1,goodsParent);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


}
